package com.ramnight.sql2.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

public class SqlKit {
	public static String wrapCol(String word){
		return "`" + word + "`";
	}
	
	public static String wrapBlank(String word){
		return " " + word + " ";
	}
	
	private static String[] concatForInsert(Set<String> keySet){
		StringBuilder keys = new StringBuilder();
		StringBuilder values = new StringBuilder();
		for(String key : keySet){
			keys.append(wrapCol(key)).append(",");
			values.append("?").append(",");
		}
		keys = wrapCurve(StrKit.removeLastComma(keys));
		values = wrapCurve(StrKit.removeLastComma(values));
		return new String[]{keys.toString(), values.toString()};
	}
	
	private static String concatForUpdate(Set<String> keySet){
		StringBuilder sb = new StringBuilder();
		for(String key : keySet){
			if("id".equals("key")){
				continue;
			}
			sb.append(wrapCol(key)).append("=?").append(",");
		}
		return StrKit.removeLastComma(sb).toString();
	}
	
	public static String concatLong(Long...longs){
		return StrKit.concat(null, String.valueOf(longs));
	}
	
	public static String concatString(String...strs){
		return StrKit.concat("'", strs);
	}
	
	public static String concatCol(String...strs){
		return StrKit.concat("`", strs);
	}
	
	public static String concat(String...strs){
		return StrKit.concat(null, strs);
	}
	
	public static String wrapCurve(String str){
		return"(" + str + ")";
	}
	
	public static StringBuilder wrapCurve(StringBuilder sb){
		return sb.insert(0, "(").append(")");
	}
	
	public static Object[] join(Object o, Collection<Object> c){
		ArrayList<Object> list = new ArrayList<>();
		list.add(o);
		list.addAll(c);
		return list.toArray();
	}
	
	public static Object[] join(Collection<Object> c1, Collection<Object> c2){
		ArrayList<Object> list = new ArrayList<>();
		list.addAll(c1);
		list.addAll(c2);
		return list.toArray();
	}
	
	public static String generateInsertSql(String table, Map<String, Object> propMap){
		StringBuilder sql = new StringBuilder();
		String[] kv = concatForInsert(propMap.keySet());
		
		sql.append("insert into ").append(wrapCol(table))
			.append(" ").append(kv[0]).append(" values ").append(kv[1]);
		return sql.toString();
	}
	

	public static String generateReplaceSql(String table, Map<String, Object> propMap){
		StringBuilder sql = new StringBuilder();
		String[] kv = concatForInsert(propMap.keySet());
		
		sql.append("replace into ").append(wrapCol(table))
			.append(" ").append(kv[0]).append(" values ").append(kv[1]);
		return sql.toString();
	}
	
	public static String generateUpdateSql(String table, Map<String, Object> propMap, String where){
		StringBuilder sql = new StringBuilder();
		String setPart = concatForUpdate(propMap.keySet());
		
		sql.append("update ").append(wrapCol(table))
			.append(" set ").append(setPart).append(where);
		return sql.toString();
	}
	
	public static String generateDeleteSql(String table, String where){
		StringBuilder sql = new StringBuilder();
		
		sql.append("delete from ").append(wrapCol(table)).append(where);
		return sql.toString();
	}
	
	public static String concatPlaceholder(int size){
		StringBuilder sb = new StringBuilder();
		int i = 0;
		while(i<size){
			sb.append('?').append(',');
			i++;
		}
		return StrKit.removeLastComma(sb).toString();
	}
	
	public static String generateIncreaseSql(boolean isIncrease, String table, String col, String where){
		StringBuilder sql = new StringBuilder();
		sql.append("update ").append(wrapCol(table))
			.append(" set ").append(wrapCol(col)).append(" = ")
			.append(wrapCol(col)).append(isIncrease ? "+" : "-").append("?").append(where);
		return sql.toString();
	}
	
	public static String as(String sql, String as){
		return wrapCurve(sql) + " as " + as;
	}
}
